package com.jlkf.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jlkf.dao.TblIntegralSetMapper;
import com.jlkf.dao.TblMessageMapper;
import com.jlkf.dao.TblUserMapper;
import com.jlkf.dao.TblUserPurseInfoMapper;
import com.jlkf.dao.TblUserTaskMapper;
import com.jlkf.pojo.TblIntegralSet;
import com.jlkf.pojo.TblMessage;
import com.jlkf.pojo.TblUser;
import com.jlkf.pojo.TblUserPurseInfo;
import com.jlkf.pojo.TblUserTask;
import com.jlkf.service.TaskService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Service
public class TaskServiceImpl implements TaskService {
	
	@Autowired
	private TblUserTaskMapper tblUserTaskMapper;
	
	@Autowired
	private TblIntegralSetMapper tblIntegralSetMapper;
	
	@Autowired
	private TblUserMapper tblUserMapper;
	
	@Autowired
	private TblUserPurseInfoMapper tblUserPurseInfoMapper;
	
	@Autowired
	private TblMessageMapper tblMessageMapper;

	 /**
     * pc端作业提交列表
     */
	@Override
	public ReturnData selectTaskList(Integer courseid) {
		if(courseid == null){
			return new ReturnData(12,"参数有误");
		}
		List<Map<String, Object>> lists = tblUserTaskMapper.selectTaskList(courseid);
		if(lists.size() > 0){
			return new ReturnData(lists,200,"成功");
		}
		return new ReturnData(100,"暂无数据");
	}

	@Override
	public ReturnData submitWork(String json) {
		try{
			TblUserTask task = StringTools.json2Obj(json, TblUserTask.class);
			if(StringTools.isEmpty(task.getgPath())){
				return new ReturnData(500,"作业上传路径不能为空");
			}
			if(StringTools.isEmpty(task.getgCourseId().toString())){
				return new ReturnData(500,"课程Id不能为空");
			}
			if(StringTools.isEmpty(task.getgCourseLessonId().toString())){
				return new ReturnData(500,"课时Id不能为空");
			}
			if(task.getgUserId()==null){
				return new ReturnData(500,"用户Id不能为空");
			}
			int rel = tblUserTaskMapper.insertSelective(task);
			if(rel>0){
				TblIntegralSet integralSet = tblIntegralSetMapper.selectByType(4);
				TblUser user = tblUserMapper.selectByPrimaryKey(task.getgUserId());
				//增加用户积分
				TblUser tuser = new TblUser();
				tuser.setgId(task.getgUserId());
				tuser.setgUserIntegral(user.getgUserIntegral()+integralSet.getIntegral());
				tuser.setgUserGrow(user.getgUserGrow()+10);
				int flag1 = tblUserMapper.updateByPrimaryKeySelective(tuser);		
				//新增积分明细
				TblUserPurseInfo purseInfo1 = new TblUserPurseInfo();
				purseInfo1.setgContent("作业提交");
				purseInfo1.setgCreatetime(new Date());
				purseInfo1.setgStatus(1);
				purseInfo1.setgTitle("作业提交");
				purseInfo1.setgType(2);
				purseInfo1.setgBalance(user.getgUserIntegral()+integralSet.getIntegral());
				purseInfo1.setgUserid(task.getgUserId());
				purseInfo1.setgMoney((double)integralSet.getIntegral());
				tblUserPurseInfoMapper.insertSelective(purseInfo1);
				
				int i = new Double(purseInfo1.getgBalance()).intValue();
				TblMessage record3 = new TblMessage();
    			record3.setgContent("作业提交获得"+integralSet.getIntegral()+"积分,现有积分"+i);
    			record3.setgTitle("积分提示");
    			record3.setgType(2);
    			record3.setgUserId(task.getgUserId());
    			record3.setgCreatetime(new Date());
    			record3.setgVersion(1);
    			tblMessageMapper.insertSelective(record3);
				
				//新增成长值明细
				TblUserPurseInfo purseInfo = new TblUserPurseInfo();
				purseInfo.setgContent("作业提交");
				purseInfo.setgCreatetime(new Date());
				purseInfo.setgStatus(1);
				purseInfo.setgTitle("作业提交");
				purseInfo.setgType(3);
				purseInfo.setgBalance((double)tuser.getgUserGrow());
				purseInfo.setgUserid(task.getgUserId());
				purseInfo.setgMoney(10.0);
				tblUserPurseInfoMapper.insertSelective(purseInfo);
				return new ReturnData(200,"作业提交成功");
			}else{
				return new ReturnData(500,"传入值有误");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}

}
